package com.ssy.system.realm;


import com.ssy.common.Constant;
import com.ssy.common.utils.ActiveUser;
import com.ssy.common.utils.Result;
import com.ssy.system.pojo.Role;
import com.ssy.system.pojo.User;
import com.ssy.system.service.IPermissionService;
import com.ssy.system.service.IRoleService;
import com.ssy.system.service.IUserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author sunshuaiyong
 * @ClassName UserRealm
 * @description: TODO
 * @date 2021/10/29 10:42
 */
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private IUserService userService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    /**
     * 认证
     * @param token
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        User user = userService.QueryUserByUserName(token.getPrincipal().toString());
        if (user != null) {
            ActiveUser activeUser = new ActiveUser();
            activeUser.setUser(user);
            Integer type = user.getType();
            if (Constant.USER_TYPE_SYSTEM.equals(type) || Constant.USER_TYPE_SYSTEM_USER.equals(type)) {
                List<String> roles = roleService.queryRoleNamesByUserId(user.getId());
                activeUser.setRoles(roles);
                List<String> permissions = permissionService.queryPermissionByUserId(user.getId());
                activeUser.setPermissions(permissions);
            }
            ByteSource salt = ByteSource.Util.bytes(user.getSalt());
            return new SimpleAuthenticationInfo(activeUser, user.getPassword(), salt, getName());
        }
        return null;
    }


    /**
     * 授权
     * @param principals
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        ActiveUser activeUser = (ActiveUser) principals.getPrimaryPrincipal();
        //如果是超级管理员就拥有全部权限
        if (Constant.USER_TYPE_SUPER.equals(activeUser.getUser().getType())) {
            info.addStringPermission("*:*");
        } else {
            List<String> roles = activeUser.getRoles();
            List<String> permissions = activeUser.getPermissions();
            if (roles != null && roles.size() > 0) {
                info.addRoles(roles);
            }
            if (permissions != null && permissions.size() > 0) {
                info.addStringPermissions(permissions);
            }
        }
        return info;
    }
}
